﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using System.Threading.Tasks;
using d1Common.WebController;
using System.Data.Entity;
using d1Common;

namespace d1Project.Modules.Business.doXZ.Controllers.a
{
    [Route("doXZ/a/N_DataFile")]
    public class XZ_N_DataFileController : ControllerBase
    {
        protected override DbContext dbContext
        {
            get
            {
                return null;
            }
        }

        public async Task<IHttpActionResult> Post()
        {
            if (!Request.Content.IsMimeMultipartContent())
            {
                throw new HttpResponseException(HttpStatusCode.UnsupportedMediaType);
            }

            var provider = await Request.Content.ReadAsMultipartAsync();
            var httpContent = provider.Contents[0];
            var stream = await httpContent.ReadAsStreamAsync();

            byte[] content = new byte[stream.Length];
            stream.Read(content, 0, content.Length);
            string path = DateTime.Now.ToString("yyyyMMdd") + "/" + doServices.Instance.DoCore.getGUID();

            var fileName = httpContent.Headers.ContentDisposition.FileName;
            if (fileName.StartsWith("\"")) fileName = fileName.Substring(1, fileName.Length - 1);
            if (fileName.EndsWith("\"")) fileName = fileName.Substring(0, fileName.Length - 1);

            string extension = fileName.Split('.')[1];
            path = path + "." + extension;
            string _path = doServices.Instance.DoStorage.writeFile(doXZDbModule.ModuleIdDefine, path, content);

            return Ok(new { Path = _path });
        }
    }
}
